package cn.sunlight.mm.base;

import cn.sunlight.mm.pojo.User;
import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static cn.sunlight.mm.common.GlobalConst.HEADER_AUTHORIZATION;

/**
 * @author sunlight
 * @description: 控制器基类  提供所有控制器类的公共方法
 * @date 2021/5/17
 * @Version v1.0
 */
public class BaseController {

    public void printResult(HttpServletResponse response, Object obj) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        JSON.writeJSONString(response.getWriter(),obj);
    }

    public <T> T parseJSON2Object(HttpServletRequest request, Class<T> tClass) throws IOException{
        // 把表单数据之间转对象
        return JSON.parseObject(request.getInputStream(),tClass);
    }

    public User getSessionUser(HttpServletRequest request, String userKey){
        if(request.getSession(false) != null){
            return (User)request.getSession(false).getAttribute(userKey);
        }
        return null;
    }

    public String getHeaderAuthorization(HttpServletRequest request){
        return request.getHeader(HEADER_AUTHORIZATION);
    }

}
